﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Labb_3.Model;

namespace Labb3.Controller
{
    public class MasterController : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private Model.BallSimulation _ballSimulation;
        private View.BallView _ballView;
        private List<View.CustomSystem> _customSystemList = new List<View.CustomSystem>();

        private MouseState _oldMousestate;

        public MasterController()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            //this.IsMouseVisible = true;
            _oldMousestate = Mouse.GetState();
            _ballSimulation = new Model.BallSimulation();
            _ballView = new View.BallView(_ballSimulation, graphics, 600, 600);
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            if(_ballSimulation != null)
            {
                _ballView.LoadContent(this.Content);
            }
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            CheckInput();
            Vector2 visualMousePos = new Vector2(_oldMousestate.X, _oldMousestate.Y);
            Vector2 logicalMousePos = _ballView._camera.GetLogicalCoords(visualMousePos);
            MouseRectangle mouseRect = MouseRectangle.GetMouseRectangle(new Vector2(logicalMousePos.X, logicalMousePos.Y));
            _ballSimulation.Update(logicalMousePos, mouseRect);
            foreach (View.CustomSystem cs in _customSystemList)
            {
                cs.Update(gameTime);
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            _ballView.Draw(this.spriteBatch);
            foreach(View.CustomSystem cs in _customSystemList)
            {
                cs.Draw(this.spriteBatch);
            }
            base.Draw(gameTime);
            spriteBatch.End();
        }

        private void CheckInput()
        {
            KeyboardState kState = Keyboard.GetState();
            MouseState newMouseState = Mouse.GetState();

            if (newMouseState.LeftButton == ButtonState.Released && _oldMousestate.LeftButton == ButtonState.Pressed && this.IsActive)
            {
                MouseRectangle mr = MouseRectangle.GetMouseRectangle(new Vector2(_oldMousestate.X, _oldMousestate.Y));
                View.CustomSystem cs = new View.CustomSystem(_ballView._camera, new Vector2(newMouseState.X, newMouseState.Y), mr.Size);
                cs.LoadContent(this.Content);
                _customSystemList.Add(cs);                
                _ballSimulation.CheckForKill();
            }
            _oldMousestate = newMouseState;
        }
    }
}

